package io.renren.modules.hospital.service.impl;

import io.renren.common.exception.RRException;
import io.renren.common.validator.Assert;
import io.renren.modules.app.form.LoginForm;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.hospital.dao.YsDao;
import io.renren.modules.hospital.entity.YsEntity;
import io.renren.modules.hospital.service.YsService;


@Service("ysService")
public class YsServiceImpl extends ServiceImpl<YsDao, YsEntity> implements YsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<YsEntity> page = this.page(
                new Query<YsEntity>().getPage(params),
                new QueryWrapper<YsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public YsEntity queryByMobile(String mobile) {
        return baseMapper.selectOne(new QueryWrapper<YsEntity>().eq("ys_phone", mobile));
    }

    @Override
    public long login(LoginForm form) {
        YsEntity user = queryByMobile(form.getMobile());
        Assert.isNull(user, "手机号或密码错误");

        //密码错误
        if(!user.getYsPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
            throw new RRException("手机号或密码错误");
        }

        return user.getYsId();
    }

}
